package com.breakfast.fun.adapter.user;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseListAdapter;
import com.breakfast.fun.data.entity.user.coupon.Coupon;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yukai on 2016/11/10.
 */

public class CouponUselessAdapter extends BaseListAdapter<Coupon.CouponData> {


    //表示是否展开的使用规则RelativeLayout对应tag的id集合
    private List<Integer> list = new ArrayList<Integer>();
    private double totalPrice = 0;
    private final static int YESCOUPON = 1003;

    private static Coupon.CouponData curCoupon;
    private static Boolean isFromUser = false;


    private static TextView checkedTv;


    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public CouponUselessAdapter(Context context, List<Coupon.CouponData> list) {
        super(context, list);
    }


    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_coupon_useless, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        Coupon.CouponData data = getItem(i);
        holder.mTvUserCouponName.setText(data.getName());
        holder.mTvUserCouponConditions.setText("满" + data.getConditions() + "使用");
        holder.mTvUserCouponValue.setText(data.getValue() + "");
        holder.mTvUserCouponBettwenTime.setText(data.getBettwenTime());
        holder.mTvUserCouponRule.setText(data.getRule().replace("|", "\n"));

        holder.mRlItemCouponRule.setTag(i);
        holder.mTvUserCouponName.setTag(data.getId());


        //进入页面时显示正确
        if (list.contains((Integer) holder.mRlItemCouponRule.getTag())) {
            holder.mIvUserCouponRuleArrow.setImageResource(R.drawable.ic_arrow_down_gray);
            holder.mLlItemCouponUselessRule.setVisibility(View.VISIBLE);
        } else {
            holder.mIvUserCouponRuleArrow.setImageResource(R.drawable.ic_arrow_right_gray);
            holder.mLlItemCouponUselessRule.setVisibility(View.GONE);
        }

        //点击时显示正确
        holder.mRlItemCouponRule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RelativeLayout v = (RelativeLayout) view;
                if (!list.contains(v.getTag())) {
                    list.add((Integer) v.getTag());
                    holder.mLlItemCouponUselessRule.setVisibility(View.VISIBLE);
                    holder.mIvUserCouponRuleArrow.setImageResource(R.drawable.ic_arrow_down_gray);
                } else {
                    list.remove((Integer) v.getTag());
                    holder.mLlItemCouponUselessRule.setVisibility(View.GONE);
                    holder.mIvUserCouponRuleArrow.setImageResource(R.drawable.ic_arrow_right_gray);
                }
            }
        });


        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.tv_user_coupon_value)
        TextView mTvUserCouponValue;
        @Bind(R.id.tv_user_coupon_conditions)
        TextView mTvUserCouponConditions;
        @Bind(R.id.tv_user_coupon_name)
        TextView mTvUserCouponName;
        @Bind(R.id.tv_user_coupon_bettwenTime)
        TextView mTvUserCouponBettwenTime;
        @Bind(R.id.ll_user_coupon_select)
        LinearLayout mLlUserCouponSelect;
        @Bind(R.id.iv_user_coupon_rule_arrow)
        ImageView mIvUserCouponRuleArrow;
        @Bind(R.id.rl_item_coupon_rule)
        RelativeLayout mRlItemCouponRule;
        @Bind(R.id.ll_user_item_coupon)
        LinearLayout mLlUserItemCoupon;
        @Bind(R.id.tv_user_coupon_rule)
        TextView mTvUserCouponRule;
        @Bind(R.id.rl_user_item_coupon)
        RelativeLayout mRlUserItemCoupon;
        @Bind(R.id.tv_sign)
        TextView mTvSign;
        @Bind(R.id.ll_item_coupon_useless_rule)
        LinearLayout mLlItemCouponUselessRule;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
